/**
 * Created by WLG on 2018/12/4.
 */
/**
 * Created by WLG on 2018/11/9.
 */
let ChatBusinessService = require("../services/chatBusinessService");
let ChatMemInfoService = require("../services/chatMemInfoService");
let ChatMsgService = require("../services/chatMsgService");
let ServerConfig = require('../config/serverConfig');
let ObsManager = require('../lib/obsManager');
let Router = require('koa-router');
let fs = require('fs');
let AccountMemService = require('../services/accountMemService');
let ThirdApiService = require('../services/thirdApiService');
let AjaxResponse = require('../model/sysAjaxResponse');
let CommonUtil = require('../lib/commonUtil');
let SysImInitMode = require('../model/sysImInitMode');
let SystemConst = require('../config/systemConst');
class ThirdApiHandler {
    static attach(app,io){
        let router = new Router();
        router
        /**
         * accountID:  "A111",
         * username: "111",
         * name: "王鹏",
         * avatar: "http://www.app4cm.cn/tocim/head/111.png"
         */
        .post([ServerConfig.httpNameSpace,"thirdApi","createAccountMem"].join("/") , async (ctx, next) => {
            let param = ctx.request.body;
            let result = await AccountMemService.save(param);
            let ar = new AjaxResponse();
            ar.data = { 'accountMemId' : result._id };
            ctx.response.body = ar;
            await next();
        })
        /**
        req:{
            accountMemId:xxx,
            operParam:{
                shareToAccountMemIds:[],
                chatMsg:{
                    msgType:'30003',
                    chatData:{
                        mainImage:xxx,
                        title:xxx,
                        URL:xxx,
                        desc:xxx,
                    }
                }
                isUnique:false,
                uniqueId:xxx
            },
            thirdInfo:{
            }
        },
        */
        .post([ServerConfig.httpNameSpace,"thirdApi","shareMsg"].join("/") , async (ctx, next) => {
            let param = ctx.request.body;
            /**
             {
                memInfo:{xxx},
                modeType:'share',
                modeInfo:{
                    shareToAccountMemIds:[],
                    chatMsg:{
                        msgType:xxx,
                        chatData:{
                            mainImage:xxx,
                            title:xxx,
                            URL:xxx,
                            desc:xxx,
                        },
                    },
                },
                thirdInfo:{
                }
             }
             */
            let imToken = ThirdApiService.createImToken();
            let imInitMode = new SysImInitMode(
                //memInfo
                await AccountMemService.getCache(param.accountMemId,false),
                //modeType
                SystemConst.ImInitModeType.Share,
                //modeInfo
                param.operParam,
                //thirdInfo
                param.thirdInfo
            );
            ThirdApiService.setImInitModeCache(imToken,imInitMode);
            let ar = new AjaxResponse();
            ar.data = { 'imToken' : imToken };
            ctx.response.body = ar;
            await next();
        });

        app.use(router.routes()).use(router.allowedMethods());
    };
}
module["exports"] = ThirdApiHandler;